package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.*;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IClazzService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ICourseService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IExamInfoService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ExamInfo")
public class ExamInfoController {
    @Autowired
    private IExamInfoService examInfoService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IClazzService clazzService;
    @Autowired
    private ITeacherService teacherService;

    @RequestMapping("/queryAllExamInfo")
    public String queryAllExamInfo(Model model,@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
                                   @RequestParam(value = "pageSize",defaultValue = "5") int pageSize)
    {
        int totalCount = examInfoService.queryExamInfoCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);
        List<Map<String,Object>> returnexaminfos = new ArrayList<>();
        List<Examinfo> examinfos = examInfoService.queryExamInfoByPage(pageNow,pageSize);
        if(!examinfos.isEmpty()&&examinfos!=null)
        {
            for(Examinfo examinfo : examinfos)
            {
                Map<String,Object> map = new HashMap<>();
                map.put("id",examinfo.getId());
                map.put("coursename",courseService.queryCourseById(examinfo.getCourseId()).getName());
                map.put("clazzname",clazzService.queryClazzById(examinfo.getClazzId()).getName());
                map.put("teachername",teacherService.queryTeacherById(examinfo.getTeacherId()).getName());
                map.put("studentNum",examinfo.getStudentNum());
                map.put("classroom",examinfo.getClassroom());
                map.put("day",examinfo.getDay());
                map.put("beginTime",examinfo.getBeginTime());
                map.put("endTime",examinfo.getEndTime());
                map.put("note",examinfo.getNote());
                returnexaminfos.add(map);
            }
        }
        model.addAttribute("examinfos", returnexaminfos);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageNow",pageNow);
        return "examinfo";
    }

    @RequestMapping("/toQueryAllExamInfoForStudent")
    public ModelAndView toQueryAllExamInfoForStudent()
    {
        List<Clazz> clazzes = clazzService.queryAllClazz();
        ModelAndView model = new ModelAndView("toqueryexaminfoforstudent");
        model.addObject("clazzes",clazzes);
        return model;
    }

    @RequestMapping("/queryAllExamInfoForStudent")
    public ModelAndView queryAllExamInfoForStudent(Integer clazzId)
    {
        List<Examinfo> examinfos = examInfoService.queryAllExamInfoByClazzId(clazzId);
        List<Map<String,Object>> returnexaminfos = new ArrayList<>();
        if(!examinfos.isEmpty() && examinfos!=null)
        {
            for(Examinfo examinfo : examinfos)
            {
                Map<String,Object> map = new HashMap<>();
                map.put("id",examinfo.getId());
                map.put("coursename",courseService.queryCourseById(examinfo.getCourseId()).getName());
                map.put("classroom",examinfo.getClassroom());
                map.put("day",examinfo.getDay());
                map.put("beginTime",examinfo.getBeginTime());
                map.put("endTime",examinfo.getEndTime());
                map.put("note",examinfo.getNote());
                returnexaminfos.add(map);
            }
        }

        ModelAndView model = new ModelAndView("examinfoforstudent");
        model.addObject("examinfos",returnexaminfos);
        return model;
    }

    @RequestMapping(value = {"/toAddExamInfo"})
    public ModelAndView toAddExamInfo() {
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Course> courses = courseService.queryAllCourse();
        List<Clazz> clazzes = clazzService.queryAllClazz();

        ModelAndView model = new ModelAndView("toaddexaminfo");
        model.addObject("teachers", teachers);
        model.addObject("courses", courses);
        model.addObject("clazzes", clazzes);
        return model;
    }

    @RequestMapping(value = {"/toUpdateExamInfo"})
    public ModelAndView toUpdateExamInfo(Integer id) {
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Course> courses = courseService.queryAllCourse();
        List<Clazz> clazzes = clazzService.queryAllClazz();

        Examinfo examinfo = examInfoService.queryExamInfoById(id);
        ModelAndView model = new ModelAndView("toupdateexaminfo");
        model.addObject("teachers", teachers);
        model.addObject("courses", courses);
        model.addObject("clazzes", clazzes);
        model.addObject("examinfo",examinfo);
        return model;
    }

    @RequestMapping(value = {"/toUpdateExamInfoClassroom"})
    public ModelAndView toUpdateExamInfoClassroom(Integer id) {
        Examinfo examinfo = examInfoService.queryExamInfoById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("id",examinfo.getId());
        map.put("coursename",courseService.queryCourseById(examinfo.getCourseId()).getName());
        map.put("clazzname",clazzService.queryClazzById(examinfo.getClazzId()).getName());
        map.put("teachername",teacherService.queryTeacherById(examinfo.getTeacherId()).getName());
        map.put("studentNum",examinfo.getStudentNum());
        map.put("classroom",examinfo.getClassroom());
        map.put("day",examinfo.getDay());
        map.put("beginTime",examinfo.getBeginTime());
        map.put("endTime",examinfo.getEndTime());
        map.put("note",examinfo.getNote());

        ModelAndView model = new ModelAndView("toupdateexaminfoclassroom");
        model.addObject("examinfo",map);
        return model;
    }

    @RequestMapping(value = {"/toUpdateExamInfoTime"})
    public ModelAndView toUpdateExamInfoTime(Integer id) {
        Examinfo examinfo = examInfoService.queryExamInfoById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("id",examinfo.getId());
        map.put("coursename",courseService.queryCourseById(examinfo.getCourseId()).getName());
        map.put("clazzname",clazzService.queryClazzById(examinfo.getClazzId()).getName());
        map.put("teachername",teacherService.queryTeacherById(examinfo.getTeacherId()).getName());
        map.put("studentNum",examinfo.getStudentNum());
        map.put("classroom",examinfo.getClassroom());
        map.put("day",examinfo.getDay());
        map.put("beginTime",examinfo.getDay() + " " + examinfo.getBeginTime());
        map.put("endTime",examinfo.getDay() + " " + examinfo.getEndTime());
        map.put("note",examinfo.getNote());

        ModelAndView model = new ModelAndView("toupdateexaminfotime");
        model.addObject("examinfo",map);
        return model;
    }

    @ResponseBody
    @RequestMapping("queryExamInfoById")
    public Examinfo queryExamInfoById(Integer id)
    {
        return examInfoService.queryExamInfoById(id);
    }

    @ResponseBody
    @RequestMapping("insertExamInfo")
    public String insertExamInfo(Integer courseId, Integer clazzId, Integer teacherId, Integer studentNum, String classroom, String day,String beginTime,String endTime,String note)
    {
        Examinfo examinfo = new Examinfo(null,courseId,clazzId,teacherId,studentNum,classroom,day,beginTime,endTime,note);
        examInfoService.insertExamInfo(examinfo);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateExamInfoById")
    public String updateExamInfoById(Integer id,Integer courseId, Integer clazzId, Integer teacherId, Integer studentNum, String classroom, String day,String beginTime,String endTime,String note)
    {
        Examinfo examinfo = new Examinfo(id,courseId,clazzId,teacherId,studentNum,classroom,day,beginTime,endTime,note);
        examInfoService.updateExamInfoById(examinfo);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateExamInfoClassroom")
    public String updateExamInfoTime(Integer id,String classroom)
    {
        Examinfo examinfo = examInfoService.queryExamInfoById(id);
        examinfo.setClassroom(classroom);
        examInfoService.updateExamInfoById(examinfo);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateExamInfoTime")
    public String updateExamInfoTime(Integer id,String beginTime,String endTime)
    {
        Examinfo examinfo = examInfoService.queryExamInfoById(id);
        String[] beginTimes = beginTime.split(" ");
        String[] endTimes = endTime.split(" ");
        examinfo.setBeginTime(beginTimes[1]);
        examinfo.setEndTime(endTimes[1]);
        examinfo.setDay(beginTimes[0]);
        examInfoService.updateExamInfoById(examinfo);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("deleteExamInfoById")
    public String deleteExamInfoById(Integer id)
    {
        examInfoService.deleteExamInfoById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("exportExcel")
    public String exportExcel()
    {
        examInfoService.exportExcel();
        return "OK";
    }

    @ResponseBody
    @RequestMapping("importExcel")
    public String importExcel(Path path)
    {
        examInfoService.importExcel(path);
        return "OK";
    }


}
